//
//  LyricHelpers.m
//  YinYueBoFang_Forever
//
//  Created by lanou3g on 15/11/8.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "LyricHelpers.h"
#import "LyricModel.h"

@interface LyricHelpers()

@property(nonatomic,strong)NSMutableArray *lyricItemArray;

@property(nonatomic,assign)NSInteger index;


@end


@implementation LyricHelpers


+(LyricHelpers *)defaultLyricHelpers
{
    static dispatch_once_t onceToken;
    static LyricHelpers *lyricHelpers = nil;
    dispatch_once(&onceToken, ^{
        lyricHelpers = [[LyricHelpers alloc] init];
    });
    return lyricHelpers;
}

-(void)changeLyricString:(NSString *)string
{
    self.lyricItemArray = [NSMutableArray array];
    NSArray *array = [string componentsSeparatedByString:@"\n"];
    for (NSString *str in array) {
        NSArray *array = [str componentsSeparatedByString:@"]"];
        if (array.count >1) {
            NSString *timeString = array[0];
            NSString *lyricString = array[1];
            NSString *newTimeString = [timeString substringFromIndex:1];
            
            NSArray *timeArray = [newTimeString componentsSeparatedByString:@":"];
            if (timeArray.count >1) {
                float minute = [timeArray[0] floatValue];
                float second = [timeArray[1] floatValue];
                
                float time = minute *60 +second;
                
                LyricModel *model = [LyricModel lyricWithLyricString:lyricString andtime:time];
                [self.lyricItemArray addObject:model];
                
            }
            
            
        }
    }
    
}

-(NSInteger)lyricItemCount
{
    return self.lyricItemArray.count;
}

-(NSInteger)lyricItemWithTime:(float)time
{
    for (int i = 0; i < self.lyricItemArray.count ; i++  ) {
        LyricModel *model = self.lyricItemArray[i];
        if (time < model.time) {
            _index = i - 1 > 0 ? i - 1 : 0;
            break;
        }
    }
    return _index;
}

-(NSString *)lyricStringWithIndex:(NSInteger)index
{
    LyricModel *model = self.lyricItemArray[index];
    return model.lyricString;
    
}
























@end
